<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>experimental::basic_channel</title>
<link rel="stylesheet" href="../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../boost_asio.html" title="Boost.Asio">
<link rel="up" href="../reference.html" title="Reference">
<link rel="prev" href="experimental__awaitable_operators__operator_pipe__pipe_/overload6.html" title="experimental::awaitable_operators::operator|| (6 of 6 overloads)">
<link rel="next" href="experimental__basic_channel/async_receive.html" title="experimental::basic_channel::async_receive">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td>
<td align="center"><a href="../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="experimental__awaitable_operators__operator_pipe__pipe_/overload6.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="experimental__basic_channel/async_receive.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_asio.reference.experimental__basic_channel"></a><a class="link" href="experimental__basic_channel.html" title="experimental::basic_channel">experimental::basic_channel</a>
</h3></div></div></div>
<p>
        <a class="indexterm" name="boost_asio.indexterm.experimental__basic_channel"></a>
      </p>
<p>
        A channel for messages.
      </p>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span>
    <span class="keyword">typename</span> <a class="link" href="Executor1.html" title="Executor requirements">Executor</a><span class="special">,</span>
    <span class="keyword">typename</span> <span class="identifier">Traits</span><span class="special">,</span>
    <span class="keyword">typename</span><span class="special">...</span> <span class="identifier">Signatures</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">basic_channel</span>
</pre>
<h5>
<a name="boost_asio.reference.experimental__basic_channel.h0"></a>
        <span class="phrase"><a name="boost_asio.reference.experimental__basic_channel.types"></a></span><a class="link" href="experimental__basic_channel.html#boost_asio.reference.experimental__basic_channel.types">Types</a>
      </h5>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Name
                </p>
              </th>
<th>
                <p>
                  Description
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel__rebind_executor.html" title="experimental::basic_channel::rebind_executor"><span class="bold"><strong>rebind_executor</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Rebinds the channel type to another executor.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/executor_type.html" title="experimental::basic_channel::executor_type"><span class="bold"><strong>executor_type</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  The type of the executor associated with the channel.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/traits_type.html" title="experimental::basic_channel::traits_type"><span class="bold"><strong>traits_type</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  The traits type associated with the channel.
                </p>
              </td>
</tr>
</tbody>
</table></div>
<h5>
<a name="boost_asio.reference.experimental__basic_channel.h1"></a>
        <span class="phrase"><a name="boost_asio.reference.experimental__basic_channel.member_functions"></a></span><a class="link" href="experimental__basic_channel.html#boost_asio.reference.experimental__basic_channel.member_functions">Member
        Functions</a>
      </h5>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Name
                </p>
              </th>
<th>
                <p>
                  Description
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/async_receive.html" title="experimental::basic_channel::async_receive"><span class="bold"><strong>async_receive</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Asynchronously receive a message.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/async_send.html" title="experimental::basic_channel::async_send"><span class="bold"><strong>async_send</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Asynchronously send a message.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/basic_channel.html" title="experimental::basic_channel::basic_channel"><span class="bold"><strong>basic_channel</strong></span></a> <span class="silver">[constructor]</span>
                </p>
              </td>
<td>
                <p>
                  Construct a basic_channel. <br> <span class="silver"> —</span><br> Construct and open a basic_channel.
                  <br> <span class="silver"> —</span><br> Move-construct a basic_channel from another.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/cancel.html" title="experimental::basic_channel::cancel"><span class="bold"><strong>cancel</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Cancel all asynchronous operations waiting on the channel.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/capacity.html" title="experimental::basic_channel::capacity"><span class="bold"><strong>capacity</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Get the capacity of the channel's buffer.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/close.html" title="experimental::basic_channel::close"><span class="bold"><strong>close</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Close the channel.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/get_executor.html" title="experimental::basic_channel::get_executor"><span class="bold"><strong>get_executor</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Get the executor associated with the object.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/is_open.html" title="experimental::basic_channel::is_open"><span class="bold"><strong>is_open</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Determine whether the channel is open.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/operator_eq_.html" title="experimental::basic_channel::operator="><span class="bold"><strong>operator=</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Move-assign a basic_channel from another.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/ready.html" title="experimental::basic_channel::ready"><span class="bold"><strong>ready</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Determine whether a message can be received without blocking.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/reset.html" title="experimental::basic_channel::reset"><span class="bold"><strong>reset</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Reset the channel to its initial state.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/try_receive.html" title="experimental::basic_channel::try_receive"><span class="bold"><strong>try_receive</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Try to receive a message without blocking.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/try_send.html" title="experimental::basic_channel::try_send"><span class="bold"><strong>try_send</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Try to send a message without blocking.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/try_send_n.html" title="experimental::basic_channel::try_send_n"><span class="bold"><strong>try_send_n</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Try to send a number of messages without blocking.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/try_send_n_via_dispatch.html" title="experimental::basic_channel::try_send_n_via_dispatch"><span class="bold"><strong>try_send_n_via_dispatch</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Try to send a number of messages without blocking, using dispatch
                  semantics to call the receive operations' completion handlers.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/try_send_via_dispatch.html" title="experimental::basic_channel::try_send_via_dispatch"><span class="bold"><strong>try_send_via_dispatch</strong></span></a>
                </p>
              </td>
<td>
                <p>
                  Try to send a message without blocking, using dispatch semantics
                  to call the receive operation's completion handler.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <a class="link" href="experimental__basic_channel/_basic_channel.html" title="experimental::basic_channel::~basic_channel"><span class="bold"><strong>~basic_channel</strong></span></a> <span class="silver">[destructor]</span>
                </p>
              </td>
<td>
                <p>
                  Destructor.
                </p>
              </td>
</tr>
</tbody>
</table></div>
<p>
        The <a class="link" href="experimental__basic_channel.html" title="experimental::basic_channel"><code class="computeroutput"><span class="identifier">experimental</span><span class="special">::</span><span class="identifier">basic_channel</span></code></a> class template is
        used for sending messages between different parts of the same application.
        A <span class="emphasis"><em>message</em></span> is defined as a collection of arguments to
        be passed to a completion handler, and the set of messages supported by a
        channel is specified by its <code class="computeroutput"><span class="identifier">Traits</span></code>
        and <code class="computeroutput"><span class="identifier">Signatures</span><span class="special">...</span></code>
        template parameters. Messages may be sent and received using asynchronous
        or non-blocking synchronous operations.
      </p>
<p>
        Unless customising the traits, applications will typically use the <code class="computeroutput"><span class="identifier">experimental</span><span class="special">::</span><span class="identifier">channel</span></code> alias template. For example:
      </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">send_loop</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">steady_timer</span><span class="special">&amp;</span> <span class="identifier">timer</span><span class="special">,</span>
    <span class="identifier">channel</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">(</span><span class="identifier">error_code</span><span class="special">,</span> <span class="keyword">int</span><span class="special">)&gt;&amp;</span> <span class="identifier">ch</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">if</span> <span class="special">(</span><span class="identifier">i</span> <span class="special">&lt;</span> <span class="number">10</span><span class="special">)</span>
  <span class="special">{</span>
    <span class="identifier">timer</span><span class="special">.</span><span class="identifier">expires_after</span><span class="special">(</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">seconds</span><span class="special">(</span><span class="number">1</span><span class="special">));</span>
    <span class="identifier">timer</span><span class="special">.</span><span class="identifier">async_wait</span><span class="special">(</span>
        <span class="special">[</span><span class="identifier">i</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">timer</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">ch</span><span class="special">](</span><span class="identifier">error_code</span> <span class="identifier">error</span><span class="special">)</span>
        <span class="special">{</span>
          <span class="keyword">if</span> <span class="special">(!</span><span class="identifier">error</span><span class="special">)</span>
          <span class="special">{</span>
            <span class="identifier">ch</span><span class="special">.</span><span class="identifier">async_send</span><span class="special">(</span><span class="identifier">error_code</span><span class="special">(),</span> <span class="identifier">i</span><span class="special">,</span>
                <span class="special">[</span><span class="identifier">i</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">timer</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">ch</span><span class="special">](</span><span class="identifier">error_code</span> <span class="identifier">error</span><span class="special">)</span>
                <span class="special">{</span>
                  <span class="keyword">if</span> <span class="special">(!</span><span class="identifier">error</span><span class="special">)</span>
                  <span class="special">{</span>
                    <span class="identifier">send_loop</span><span class="special">(</span><span class="identifier">i</span> <span class="special">+</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">timer</span><span class="special">,</span> <span class="identifier">ch</span><span class="special">);</span>
                  <span class="special">}</span>
                <span class="special">});</span>
          <span class="special">}</span>
        <span class="special">});</span>
  <span class="special">}</span>
  <span class="keyword">else</span>
  <span class="special">{</span>
    <span class="identifier">ch</span><span class="special">.</span><span class="identifier">close</span><span class="special">();</span>
  <span class="special">}</span>
<span class="special">}</span>

<span class="keyword">void</span> <span class="identifier">receive_loop</span><span class="special">(</span><span class="identifier">channel</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">(</span><span class="identifier">error_code</span><span class="special">,</span> <span class="keyword">int</span><span class="special">)&gt;&amp;</span> <span class="identifier">ch</span><span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">ch</span><span class="special">.</span><span class="identifier">async_receive</span><span class="special">(</span>
      <span class="special">[&amp;</span><span class="identifier">ch</span><span class="special">](</span><span class="identifier">error_code</span> <span class="identifier">error</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">i</span><span class="special">)</span>
      <span class="special">{</span>
        <span class="keyword">if</span> <span class="special">(!</span><span class="identifier">error</span><span class="special">)</span>
        <span class="special">{</span>
          <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Received "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span> <span class="special">&lt;&lt;</span> <span class="string">"\n"</span><span class="special">;</span>
          <span class="identifier">receive_loop</span><span class="special">(</span><span class="identifier">ch</span><span class="special">);</span>
        <span class="special">}</span>
      <span class="special">});</span>
<span class="special">}</span>
</pre>
<h5>
<a name="boost_asio.reference.experimental__basic_channel.h2"></a>
        <span class="phrase"><a name="boost_asio.reference.experimental__basic_channel.thread_safety"></a></span><a class="link" href="experimental__basic_channel.html#boost_asio.reference.experimental__basic_channel.thread_safety">Thread
        Safety</a>
      </h5>
<p>
        <span class="emphasis"><em>Distinct</em></span> <span class="emphasis"><em>objects:</em></span> Safe.
      </p>
<p>
        <span class="emphasis"><em>Shared</em></span> <span class="emphasis"><em>objects:</em></span> Unsafe.
      </p>
<p>
        The <a class="link" href="experimental__basic_channel.html" title="experimental::basic_channel"><code class="computeroutput"><span class="identifier">experimental</span><span class="special">::</span><span class="identifier">basic_channel</span></code></a> class template is
        not thread-safe, and would typically be used for passing messages between
        application code that runs on the same thread or in the same strand. Consider
        using <a class="link" href="experimental__basic_concurrent_channel.html" title="experimental::basic_concurrent_channel"><code class="computeroutput"><span class="identifier">experimental</span><span class="special">::</span><span class="identifier">basic_concurrent_channel</span></code></a>, and its
        alias template <code class="computeroutput"><span class="identifier">experimental</span><span class="special">::</span><span class="identifier">concurrent_channel</span></code>,
        to pass messages between code running in different threads.
      </p>
<h5>
<a name="boost_asio.reference.experimental__basic_channel.h3"></a>
        <span class="phrase"><a name="boost_asio.reference.experimental__basic_channel.requirements"></a></span><a class="link" href="experimental__basic_channel.html#boost_asio.reference.experimental__basic_channel.requirements">Requirements</a>
      </h5>
<p>
        <span class="emphasis"><em>Header: </em></span><code class="literal">boost/asio/experimental/basic_channel.hpp</code>
      </p>
<p>
        <span class="emphasis"><em>Convenience header: </em></span>None
      </p>
</div>
<div class="copyright-footer">Copyright © 2003-2025 Christopher M.
      Kohlhoff<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="experimental__awaitable_operators__operator_pipe__pipe_/overload6.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="experimental__basic_channel/async_receive.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
